package com.lqg.bookLibrary.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.lqg.bookLibrary.common.core.domain.JsonResult;
import com.lqg.bookLibrary.common.myInterface.CurrentUser;
import com.lqg.bookLibrary.common.myInterface.CurrentUserPojo;
import com.lqg.bookLibrary.pojo.Books;
import com.lqg.bookLibrary.pojo.Collect;
import com.lqg.bookLibrary.pojo.Type;
import com.lqg.bookLibrary.service.IBooksService;
import com.lqg.bookLibrary.service.ICollectService;
import com.lqg.bookLibrary.service.ITypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.web.PortResolverImpl;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/collect")
@CrossOrigin
public class CollectController {

    @Autowired
    private ICollectService collectService;

    @Autowired
    private IBooksService booksService;

    @Autowired
    private ITypeService typeService;

    @GetMapping("/list")
    public JsonResult getListById(@CurrentUser CurrentUserPojo userPojo) {
        Long userId = userPojo.getUserId();
        List<Collect> list = collectService.lambdaQuery().eq(Collect::getUserId, userId).list();

        if (list.size() == 0) {
            return JsonResult.success();
        }

        List<Books> booksList = booksService.lambdaQuery().in(Books::getBookId,
                list.stream().map(x -> x.getBookId()).collect(Collectors.toList())
        ).list();

        List<Type> typeList = typeService.lambdaQuery().in(Type::getTypeId,
                booksList.stream().map(x -> x.getTypeId()).collect(Collectors.toList())
        ).list();

        List<JSONObject> result = new ArrayList<>();

        result = list.stream().map(x -> {
            Books temp = booksList.stream().filter(y -> Objects.equals(y.getBookId(), x.getBookId()))
                    .findFirst().get();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("collectId", x.getCollectId());
            jsonObject.put("title", temp.getTitle());
            jsonObject.put("imageUrl", temp.getImageUrl());
            jsonObject.put("type",
                    typeList.stream().filter(y -> Objects.equals(y.getTypeId(), temp.getTypeId()))
                            .findFirst().get().getTypeName()
            );
            jsonObject.put("createdAt", x.getCreatedAt());
            return jsonObject;
        }).collect(Collectors.toList());

        Collections.reverse(result);

        return JsonResult.success(result);
    }

    @PostMapping("/del")
    public JsonResult delById(@RequestBody String post) {
        JSONObject jsonObject = JSON.parseObject(post);
        List<Long> delList = (List) jsonObject.get("delList");
        collectService.removeBatchByIds(delList);
        return JsonResult.success();
    }

    @GetMapping ("/add/{bookId}")
    public JsonResult addById(@PathVariable Long bookId, @CurrentUser CurrentUserPojo userPojo) {
        Long userId = userPojo.getUserId();
        boolean exists = collectService.lambdaQuery()
                .eq(Collect::getBookId, bookId)
                .eq(Collect::getUserId, userId)
                .exists();
        if (exists) {
            Collect one = collectService.lambdaQuery()
                    .eq(Collect::getBookId, bookId)
                    .eq(Collect::getUserId, userId)
                    .one();
            collectService.removeById(one.getCollectId());
        }else {
            collectService.save(new Collect(userId, bookId));
        }
        return JsonResult.success();
    }

    @GetMapping ("/select/{bookId}")
    public JsonResult selectById(@PathVariable Long bookId, @CurrentUser CurrentUserPojo userPojo) {
        Long userId = userPojo.getUserId();
        Boolean one = collectService.lambdaQuery()
                .eq(Collect::getBookId, bookId)
                .eq(Collect::getUserId, userId)
                .exists();
        if (!one) {
            return JsonResult.success(0);
        } else {
            return JsonResult.success(1);
        }
    }

    @GetMapping ("/getBookId/{collectId}")
    public JsonResult selectById(@PathVariable Long collectId) {
        Collect one = collectService.lambdaQuery()
                .eq(Collect::getCollectId, collectId)
                .one();
        return JsonResult.success(one.getBookId());
    }



}
